package a_o2odata_deal

import com.alibaba.fastjson.{JSON, JSONObject}
import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkConf, SparkContext}
import org.elasticsearch.spark._

/**
  * @ Auther: o2o-rd-0008
  * @ Date:   2019/12/6 14:21
  * @ Param:  ${PARAM}
  * @ Description: 
  */
object modify_zhibo {

  def main(args: Array[String]): Unit = {

    val conf = new SparkConf()
    conf.setAppName(s"${this.getClass.getSimpleName}")
    conf.set("spark.debug.maxToStringFields", "500")
    //conf.setMaster("local[*]")
    conf.set("es.nodes", "192.168.1.157")
    conf.set("es.port", "9200")
    conf.set("cluster.name", "O2OElastic")
    conf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
    conf.set("spark.sql.caseSensitive", "true")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)
    sc.setLogLevel("WARN")
    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")

    /*val rdd = sc.esJsonRDD("2019_tmall/tmall_2019_11",
      """
        {
       |  "query": {
       |    "term": {
       |      "is_showLive": {
       |        "value": "true"
       |      }
       |    }
       |  }
       |}
      """.stripMargin).values
    val zhibo_data = sqlContext.read.json(rdd)*/

    val zhibo_data = sqlContext.read.json("s3a://o2o-dataproces-group/zhang_feng/create_data/2019_11/meituan/meituan_main_v3/zhibobeifen")

    //zhibo_data.repartition(4).write.json("s3a://o2o-dataproces-group/zhang_feng/create_data/2019_11/meituan/meituan_main_v3/zhibobeifen")

    zhibo_data.createOrReplaceTempView("zhibo_data")

    //加载直播数据
    val data4 = sqlContext.read.json(s"${a_aa_amainpackage.a_o2odata_deal.config.config.tmall_live_detail_path}")
    val data5 = sqlContext.read.json(s"${a_aa_amainpackage.a_o2odata_deal.config.config.tmall_live_id_path}")

    data4.createOrReplaceTempView("data4")
    data5.createOrReplaceTempView("data5")

    val data3 = sqlContext.sql(
      s"""
         |select
         |t1.*,
         |t1.fans as anchorFans,
         |t2.priceText as live_priceText,
         |t2.good_id as good_ids
         |from data5 t1
         |inner join
         |data4 t2
         |on t1.liveId=t2.liveId
       """.stripMargin).drop("good_id","fans").withColumnRenamed("good_ids","good_id").where("good_id!='-1'")

    data3.createOrReplaceTempView("data3")

    val result1 = sqlContext.sql(
      s"""
         |select
         |t1.*,
         |cast(t2.liveCount as bigint) as liveCount,
         |cast(t2.live_priceText as double) as live_priceText,
         |cast(t2.anchorFans as bigint) as anchorFans
         |from zhibo_data t1
         |left join
         |data3 t2
         |on t1.good_id=t2.good_id
       """.stripMargin).dropDuplicates("good_id")//.drop("timeStamp","sellCount").withColumn("timeStamp",lit("1575043200"))
      //.withColumnRenamed("sellCounts","sellCount")

    val value = result1.toJSON.rdd.map(line => {
      val lines = JSON.parseObject(line)
      val evaluates = lines.getOrDefault("evaluates", "-1").toString
      val Base_Info = lines.getOrDefault("Base_Info", "-1").toString
      var promotion_price = lines.getOrDefault("promotion_price","-1").toString
      var original_cost = lines.getOrDefault("original_cost","-1").toString
      val priceText = lines.get("priceText").toString
      if (promotion_price.contains("?") | promotion_price.contains("？")){
        promotion_price = priceText
      }
      if (original_cost.contains("?") | original_cost.contains("？")){
        original_cost = priceText
      }
      var ev = "-1"
      var str = new JSONObject
      if (evaluates.equals("-1")) {
        ev = "{\"fuyi\":\"-1\"}"
      } else {
        str = JSON.parseObject(evaluates)
      }
      if (!ev.contains("fuyi")) {
        lines.put("evaluates", str)
      } else {
        val evs = JSON.parseObject(ev)
        lines.put("evaluates", evs)
      }
      lines.put("Base_Info",Base_Info)
      lines.put("promotion_price",promotion_price)
      lines.put("original_cost",original_cost)
      lines
    })
    //原始代码
    value.saveToEs(s"2019_tmall/tmall_2019_11", Map("es.mapping.id" -> "good_id"))


  }
}
